<template>
  <van-tabbar v-model="active" @change="onChange" v-if="active !== -1">
    <van-tabbar-item v-for="item in menuList" :key="item">
      <span>{{ item.title }}</span>
      <template #icon="props">
        <img :src="props.active ? item.active_icon : item.icon" style="width: 20px" />
      </template>
    </van-tabbar-item>
  </van-tabbar>
</template>

<script lang="ts">
  import { defineComponent, ref, watch } from 'vue'
  import { useRoute, useRouter } from 'vue-router'

  export default defineComponent({
    setup() {
      const route = useRoute()
      const router = useRouter()
      const active = ref(0)
      const menuList = [
        {
          url: '/',
          title: '首页',
          icon: 'https://img.yzcdn.cn/vant/user-inactive.png',
          active_icon: 'https://img.yzcdn.cn/vant/user-active.png',
        },
        {
          url: '/user',
          title: '我的',
          icon: 'https://img.yzcdn.cn/vant/user-inactive.png',
          active_icon: 'https://img.yzcdn.cn/vant/user-active.png',
        },
      ]

      watch(route, (newVal: any) => {
        active.value = menuList.findIndex((val) => val.url === newVal.path)
      })
      const onChange = (index: any) => {
        router.push({ path: menuList[index].url })
      }
      return {
        route,
        router,
        active,
        menuList,
        onChange,
      }
    },
  })
</script>

<style></style>
